package com.ruoyi.play.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.play.domain.*;
import com.ruoyi.play.mapper.PlayAllMapper;
import com.ruoyi.play.mapper.PlayCopyrightMapper;
import com.ruoyi.play.mapper.PlayOperaLogMapper;
import com.ruoyi.play.service.IPlayCopyrightService;
import com.ruoyi.shortplay.domain.YoutobeDateOpera;
import com.ruoyi.shortplay.mapper.YoutobeDateOperaMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 片单版权Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-20
 */
@Service
public class PlayCopyrightServiceImpl implements IPlayCopyrightService {
    @Resource
    private PlayCopyrightMapper playCopyrightMapper;

    @Override
    public List<PlayCopyrightVO> selectPlayCopyrightListTwo(PlayCopyrightIO params) {
        return playCopyrightMapper.selectPlayCopyrightListTwo(params);
    }

    /**
     * 查询片单版权
     *
     * @param id 片单版权主键
     * @return 片单版权
     */
    @Override
    public PlayCopyright selectPlayCopyrightById(Long id) {
        return playCopyrightMapper.selectPlayCopyrightById(id);
    }

    /**
     * 查询片单版权列表
     * 
     * @param playCopyright 片单版权
     * @return 片单版权
     */
    @Override
    public List<PlayCopyright> selectPlayCopyrightList(PlayCopyright playCopyright)
    {
        return playCopyrightMapper.selectPlayCopyrightList(playCopyright);
    }

    /**
     * 新增片单版权
     * 
     * @param playCopyright 片单版权
     * @return 结果
     */
    @Override
    public int insertPlayCopyright(PlayCopyright playCopyright) {
        //新增时关联偏单为空
        playCopyright.setPlayId(null);
        playCopyright.setCreateTime(DateUtils.getNowDate());
        return playCopyrightMapper.insertPlayCopyright(playCopyright);
    }

    @Resource
    YoutobeDateOperaMapper youtobeDateOperaMapper;
    @Resource
    PlayAllMapper playAllMapper;
    @Resource
    PlayOperaLogMapper playOperaLogMapper;

    /**
     * 修改片单版权
     *
     * @param playCopyright 片单版权
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updatePlayCopyright(PlayCopyright playCopyright) {
        PlayCopyright copy = playCopyrightMapper.selectPlayCopyrightById(playCopyright.getId());

        //关联片单主键处理
        updatePlayId(playCopyright,copy);
        playCopyright.setUpdateTime(DateUtils.getNowDate());
        int i = playCopyrightMapper.updatePlayCopyright(playCopyright);
        //剪辑人不为空，则新增数据至每日操作表
        if ("Y".equals(playCopyright.getIzEdit()) && playCopyright.getOpId() != null) {
            if (copy.getPlayId() == null) {
                // 手动设置回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("关联片单为空，不能分发至操作表！");
            }
            Date now = new Date();
            PlayAll play = playAllMapper.selectPlayAllById(copy.getPlayId());
            YoutobeDateOpera in = new YoutobeDateOpera();
            in.setTitle(play.getName());
            in.setPlot(play.getContent());
            in.setType("no_edit");
            in.setOpId(playCopyright.getOpId());
            in.setPcId(playCopyright.getId());
            in.setOpTime(now);
            in.setCreateTime(now);
            youtobeDateOperaMapper.insertYoutobeDateOpera(in);
            //片单剪辑发布表数据新增
            PlayOperaLog log = new PlayOperaLog();
            log.setCreateTime(now);
            log.setEditDate(now);
            log.setPcId(in.getPcId());
            log.setPlay(play.getName());
            log.setOpId(in.getId());
            playOperaLogMapper.insertPlayOperaLog(log);
        }

        return AjaxResult.success();
    }

    private void updatePlayId(PlayCopyright params,PlayCopyright old) {
        //版权ID不为空，数据更新
        if (params.getPlayId() != null) {
            if (old.getPlayId() == null) {
                //旧数据为空，直接更新
                PlayAll play = new PlayAll();
                //版权ID不为空，版权那边数据也同时更新
                play.setCoId(params.getId());
                play.setId(params.getPlayId());
                playAllMapper.updatePlayAll(play);
            }
            if (old.getPlayId() != null && old.getPlayId() != params.getPlayId()) {
                //旧数据不为空，先清除旧数据，再更新新数据
                playAllMapper.updateOldPlayAll(old.getPlayId());

                PlayCopyright copy = new PlayCopyright();
                //版权ID不为空，版权那边数据也同时更新
                copy.setId(params.getId());
                copy.setPlayId(params.getPlayId());
                playCopyrightMapper.updatePlayCopyright(copy);
            }

        }
    }

    /**
     * 批量删除片单版权
     * 
     * @param ids 需要删除的片单版权主键
     * @return 结果
     */
    @Override
    public int deletePlayCopyrightByIds(Long[] ids)
    {
        return playCopyrightMapper.deletePlayCopyrightByIds(ids);
    }

    /**
     * 删除片单版权信息
     * 
     * @param id 片单版权主键
     * @return 结果
     */
    @Override
    public int deletePlayCopyrightById(Long id)
    {
        return playCopyrightMapper.deletePlayCopyrightById(id);
    }
}
